
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

ENT.CarHealth = 150 //150
ENT.Busted = false
ENT.Fire = false

ENT.Car = nil

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
	//local self.Entity = self.Entity
	
	self.Entity:SetMoveType( MOVETYPE_NONE )
	self.Entity:DrawShadow( false )
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableCollisions( false )		
	end

	self.Entity:SetCollisionGroup( COLLISION_GROUP_DEBRIS_TRIGGER )
	self.Entity:SetTrigger( true )
	self.Entity:SetNotSolid( true )

	util.PrecacheSound("ambient/explosions/explode_1.wav")
	util.PrecacheSound("weapons/c4/c4_explode1.wav")
	util.PrecacheSound("weapons/c4/c4_exp_deb2.wav")
end

function ENT:OnTakeDamage( dmginfo )
end

function ENT:PhysicsCollide( data, physobj )

end

function ENT:Touch(ent)
end

function ENT:Use( activator, caller )
end

function ENT:Think()
	if self.Busted == true then return end
	if self.Car != nil then
		if !self.Car:IsValid() then

		else
			
		end
	end
end

function ENT:StartBoom()
	if self.Car:GetModel() == "models/lambo/lambo.mdl" then
		//self.Car:GetDriver():Kill()
		local destroyedcar = ents.Create("fueled_gib")
		destroyedcar:SetModel("models/lambo/lambo_shell.mdl")
		destroyedcar:SetPos(self.Car:GetPos())
		destroyedcar:SetAngles(self.Car:GetAngles())
		destroyedcar:Spawn()
		destroyedcar:SetSkin(self.Car:GetSkin())
		destroyedcar:CleanUpGibs()

		local wheel_fl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fl" ) )
		local wheel_fr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fr" ) )
		local wheel_rr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rr" ) )
		local wheel_rl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rl" ) )

		local carwheel1 = ents.Create("fueled_gib")
		carwheel1:SetModel("models/lambo/lambo_wheel.mdl")
		carwheel1:SetPos(wheel_fl.Pos)
		carwheel1:SetAngles(wheel_fl.Ang)
		carwheel1:Spawn()
		carwheel1:CleanUpGibs()

		local carwheel2 = ents.Create("fueled_gib")
		carwheel2:SetModel("models/lambo/lambo_wheel.mdl")
		carwheel2:SetPos(wheel_fr.Pos)
		carwheel2:SetAngles(wheel_fr.Ang)
		carwheel2:Spawn()
		carwheel2:CleanUpGibs()

		local carwheel3 = ents.Create("fueled_gib")
		carwheel3:SetModel("models/lambo/lambo_wheel.mdl")
		carwheel3:SetPos(wheel_rr.Pos)
		carwheel3:SetAngles(wheel_rr.Ang)
		carwheel3:Spawn()
		carwheel3:CleanUpGibs()

		local carwheel4 = ents.Create("fueled_gib")
		carwheel4:SetModel("models/lambo/lambo_wheel.mdl")
		carwheel4:SetPos(wheel_rl.Pos)
		carwheel4:SetAngles(wheel_rl.Ang)
		carwheel4:Spawn()
		carwheel4:CleanUpGibs()

		self.Car:Remove()
		self:GoBoom()

		self.Busted = true
	end
	if self.Car:GetModel() == "models/caterham/caterham.mdl" then
		//self.Car:GetDriver():Kill()
		local destroyedcar = ents.Create("fueled_gib")
		destroyedcar:SetModel("models/caterham/caterham_shell.mdl")
		destroyedcar:SetPos(self.Car:GetPos())
		destroyedcar:SetAngles(self.Car:GetAngles())
		destroyedcar:Spawn()
		destroyedcar:SetSkin(self.Car:GetSkin())
		destroyedcar:CleanUpGibs()

		local wheel_fl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fl" ) )
		local wheel_fr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fr" ) )
		local wheel_rr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rr" ) )
		local wheel_rl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rl" ) )

		local carwheel1 = ents.Create("fueled_gib")
		carwheel1:SetModel("models/caterham/caterham_wheel.mdl")
		carwheel1:SetPos(wheel_fl.Pos)
		carwheel1:SetAngles(wheel_fl.Ang)
		carwheel1:Spawn()
		carwheel1:CleanUpGibs()

		local carwheel2 = ents.Create("fueled_gib")
		carwheel2:SetModel("models/caterham/caterham_wheel.mdl")
		carwheel2:SetPos(wheel_fr.Pos)
		carwheel2:SetAngles(wheel_fr.Ang)
		carwheel2:Spawn()
		carwheel2:CleanUpGibs()

		local carwheel3 = ents.Create("fueled_gib")
		carwheel3:SetModel("models/caterham/caterham_wheel.mdl")
		carwheel3:SetPos(wheel_rr.Pos)
		carwheel3:SetAngles(wheel_rr.Ang)
		carwheel3:Spawn()
		carwheel3:CleanUpGibs()

		local carwheel4 = ents.Create("fueled_gib")
		carwheel4:SetModel("models/caterham/caterham_wheel.mdl")
		carwheel4:SetPos(wheel_rl.Pos)
		carwheel4:SetAngles(wheel_rl.Ang)
		carwheel4:Spawn()
		carwheel4:CleanUpGibs()

		self.Car:Remove()
		self:GoBoom()

		self.Busted = true
	end
	if self.Car:GetModel() == "models/shelby/shelby.mdl" then
		//self.Car:GetDriver():Kill()
		local destroyedcar = ents.Create("fueled_gib")
		destroyedcar:SetModel("models/shelby/shelby_shell.mdl")
		destroyedcar:SetPos(self.Car:GetPos())
		destroyedcar:SetAngles(self.Car:GetAngles())
		destroyedcar:Spawn()
		destroyedcar:SetSkin(self.Car:GetSkin())
		destroyedcar:CleanUpGibs()

		local wheel_fl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fl" ) )
		local wheel_fr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fr" ) )
		local wheel_rr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rr" ) )
		local wheel_rl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rl" ) )

		local carwheel1 = ents.Create("fueled_gib")
		carwheel1:SetModel("models/shelby/shelby_wheel.mdl")
		carwheel1:SetPos(wheel_fl.Pos)
		carwheel1:SetAngles(wheel_fl.Ang)
		carwheel1:Spawn()
		carwheel1:CleanUpGibs()

		local carwheel2 = ents.Create("fueled_gib")
		carwheel2:SetModel("models/shelby/shelby_wheel.mdl")
		carwheel2:SetPos(wheel_fr.Pos)
		carwheel2:SetAngles(wheel_fr.Ang)
		carwheel2:Spawn()

		local carwheel3 = ents.Create("fueled_gib")
		carwheel3:SetModel("models/shelby/shelby_wheel.mdl")
		carwheel3:SetPos(wheel_rr.Pos)
		carwheel3:SetAngles(wheel_rr.Ang)
		carwheel3:Spawn()

		local carwheel4 = ents.Create("fueled_gib")
		carwheel4:SetModel("models/shelby/shelby_wheel.mdl")
		carwheel4:SetPos(wheel_rl.Pos)
		carwheel4:SetAngles(wheel_rl.Ang)
		carwheel4:Spawn()

		self.Car:Remove()
		self:GoBoom()

		self.Busted = true
	end
	if self.Car:GetModel() == "models/mini/mini.mdl" then
		//self.Car:GetDriver():Kill()
		local destroyedcar = ents.Create("fueled_gib")
		destroyedcar:SetModel("models/mini/mini_shell.mdl")
		destroyedcar:SetPos(self.Car:GetPos())
		destroyedcar:SetAngles(self.Car:GetAngles())
		destroyedcar:Spawn()
		destroyedcar:SetSkin(self.Car:GetSkin())
		destroyedcar:CleanUpGibs()

		local wheel_fl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fl" ) )
		local wheel_fr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_fr" ) )
		local wheel_rr = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rr" ) )
		local wheel_rl = self.Car:GetAttachment( self.Car:LookupAttachment( "wheel_rl" ) )

		local carwheel1 = ents.Create("fueled_gib")
		carwheel1:SetModel("models/mini/mini_wheel.mdl")
		carwheel1:SetPos(wheel_fl.Pos)
		carwheel1:SetAngles(wheel_fl.Ang)
		carwheel1:Spawn()
		carwheel1:CleanUpGibs()

		local carwheel2 = ents.Create("fueled_gib")
		carwheel2:SetModel("models/mini/mini_wheel.mdl")
		carwheel2:SetPos(wheel_fr.Pos)
		carwheel2:SetAngles(wheel_fr.Ang)
		carwheel2:Spawn()
		carwheel2:CleanUpGibs()

		local carwheel3 = ents.Create("fueled_gib")
		carwheel3:SetModel("models/mini/mini_wheel.mdl")
		carwheel3:SetPos(wheel_rr.Pos)
		carwheel3:SetAngles(wheel_rr.Ang)
		carwheel3:Spawn()
		carwheel3:CleanUpGibs()

		local carwheel4 = ents.Create("fueled_gib")
		carwheel4:SetModel("models/mini/mini_wheel.mdl")
		carwheel4:SetPos(wheel_rl.Pos)
		carwheel4:SetAngles(wheel_rl.Ang)
		carwheel4:Spawn()
		carwheel4:CleanUpGibs()

		self.Car:Remove()
		self:GoBoom()

		self.Busted = true
	end
end

function ENT:GoBoom()
	if !self.Exploded then

		self.Exploded = true

		local ar2Explo = ents.Create( "env_ar2explosion" )
		ar2Explo:SetOwner( self.Owner )
		ar2Explo:SetPos( self.Entity:GetPos() )
		ar2Explo:Spawn()
		ar2Explo:Activate()
		ar2Explo:Fire( "Explode", "", 0.5 )

		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos())
		effectdata:SetScale(1)
		util.Effect("effect_explosion_scaleableFUEL", effectdata)

		local physexplosion = ents.Create("env_physexplosion")
		physexplosion:SetPos(self:GetPos() + Vector(0,0,5))
		physexplosion:SetKeyValue("Magnitude","1000")
		physexplosion:SetKeyValue("radius","900")
		physexplosion:SetKeyValue("spawnflags","19")
		physexplosion:Spawn()
		physexplosion:Fire("explode","",0)
		physexplosion:Fire("kill","",1)

		--util.BlastDamage(self, self, self:GetPos(), 400, 200)
	
		WorldSound("ambient/explosions/explode_1.wav", self.Entity:GetPos(), 100, 100)
		WorldSound("weapons/c4/c4_explode1.wav", self.Entity:GetPos(), 100, 100)
		WorldSound("weapons/c4/c4_exp_deb2.wav", self.Entity:GetPos(), 100, 100)
	end
end

function ENT:SetCar(car)
	self.Car = car
end

function ENT:TakeHealth(amount)
	self.CarHealth = self.CarHealth - amount

	print("Did "..amount.." damage!")

	if self.CarHealth <= 0 then
		self:StartBoom()
	end
end

function ENT:GetDMGControl()
	return self.Entity
end